/* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2006-2011 Flexiant Limited.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.idas.wsdl;

import java.lang.reflect.Method;

import com.idas.wsdl.WSDLComment.COMMNT_TYPE;
import com.sun.xml.internal.ws.api.wsdl.writer.WSDLGenExtnContext;
import com.sun.xml.internal.ws.api.wsdl.writer.WSDLGeneratorExtension;
import com.sun.xml.internal.txw2.TypedXmlWriter;
import com.sun.xml.internal.ws.api.model.CheckedException;
import com.sun.xml.internal.ws.api.model.JavaMethod;

/**
 * This class extends WSDLGeneratorExtension add the comment defined in the @WSDLComment
 * annotaions on service operations and service class into the generated WSDL by
 * via JAX-WS
 * 
 * @author Gihan Munasighe
 * 
 */
public class WSDLCommentWSDLGeneratorExtension extends WSDLGeneratorExtension {

	@Override
	public void end(WSDLGenExtnContext context) {

	}

	@Override
	public void start(WSDLGenExtnContext context) {
		Class cls = context.getEndpointClass();
		WSDLComment comm = (WSDLComment) cls.getAnnotation(WSDLComment.class);
		if (comm != null) {
			String s = comm.comment();
			COMMNT_TYPE ct = comm.type();
			if (ct == COMMNT_TYPE.WSDL_DOC) {
				TypedXmlWriter doc = context.getRoot()._element(
						"documentation", TypedXmlWriter.class);
				doc._pcdata(s);
			} else {
				context.getRoot()._comment(s);
			}
		}
		super.start(context);
	}

	@Override
	public void addOperationExtension(TypedXmlWriter operation,
			JavaMethod method) {

		Method m = method.getMethod();
		WSDLComment comm = m.getAnnotation(WSDLComment.class);
		if (comm != null) {
			String s = comm.comment();
			COMMNT_TYPE ct = comm.type();
			if (ct == COMMNT_TYPE.WSDL_DOC) {
				TypedXmlWriter doc = operation._element("documentation",
						TypedXmlWriter.class);
				doc._pcdata(s);
			} else {
				operation._comment(s);
			}
		}
	}

	@Override
	public void addOperationInputExtension(TypedXmlWriter input,
			JavaMethod method) {
		Method m = method.getMethod();
		WSDLComment comm = m.getAnnotation(WSDLComment.class);
		if (comm != null) {
			String s = comm.inputcomment();
			if (s.trim().length() > 0) {
				COMMNT_TYPE ct = comm.type();
				if (ct == COMMNT_TYPE.WSDL_DOC) {
					TypedXmlWriter doc = input._element("documentation",
							TypedXmlWriter.class);
					doc._pcdata(s);
				} else {
					input._comment(s);
				}
			}
		}
	}

	@Override
	public void addOperationOutputExtension(TypedXmlWriter output,
			JavaMethod method) {
		Method m = method.getMethod();

		WSDLComment comm = m.getAnnotation(WSDLComment.class);
		if (comm != null) {
			String s = comm.outputcomment();
			if (s.trim().length() > 0) {
				COMMNT_TYPE ct = comm.type();
				if (ct == COMMNT_TYPE.WSDL_DOC) {
					TypedXmlWriter doc = output._element("documentation",
							TypedXmlWriter.class);
					doc._pcdata(s);
				} else {
					output._comment(s);
				}
			}
		}
	}

	@Override
	public void addOperationFaultExtension(TypedXmlWriter fault,
			JavaMethod method, CheckedException arg2) {
		Method m = method.getMethod();
		WSDLComment comm = m.getAnnotation(WSDLComment.class);
		if (comm != null) {
			String s = comm.faultcomment();
			if (s.trim().length() > 0) {
				COMMNT_TYPE ct = comm.type();
				if (ct == COMMNT_TYPE.WSDL_DOC) {
					TypedXmlWriter doc = fault._element("documentation",
							TypedXmlWriter.class);
					doc._pcdata(s);
				} else {
					fault._comment(s);
				}
			}
		}
		super.addOperationFaultExtension(fault, method, arg2);
	}

}